<template>
  <el-container class="container-layout">
    <el-aside :width="isOpen?'200px':'64px'">
      <div class="logo" :class="{'mini-logo':!isOpen}"></div>
      <el-menu
        router
        style="border-right:none"
        :collapse="!isOpen"
        :collapse-transition="false"
        :default-active="$route.path"
        background-color="#002233"
        text-color="#fff"
        active-text-color="#ffd04b"
      >
        <el-menu-item index="/">
          <i class="el-icon-s-home"></i>
          <span slot="title">首页</span>
        </el-menu-item>
        <el-menu-item index="/article">
          <i class="el-icon-document"></i>
          <span slot="title">内容管理</span>
        </el-menu-item>
        <el-menu-item index="/image">
          <i class="el-icon-picture"></i>
          <span slot="title">素材管理</span>
        </el-menu-item>
        <el-menu-item index="/publish">
          <i class="el-icon-s-promotion"></i>
          <span slot="title">发布文章</span>
        </el-menu-item>
        <el-menu-item index="/comment">
          <i class="el-icon-chat-dot-round"></i>
          <span slot="title">评论管理</span>
        </el-menu-item>
        <el-menu-item index="/fans">
          <i class="el-icon-present"></i>
          <span slot="title">粉丝管理</span>
        </el-menu-item>
        <el-menu-item index="/setting">
          <i class="el-icon-setting"></i>
          <span slot="title">个人设置</span>
        </el-menu-item>
      </el-menu>
    </el-aside>
    <el-container>
      <el-header>
        <span
          class="icon"
          @click="isOpen=!isOpen"
          :class="{'el-icon-s-fold':isOpen,'el-icon-s-unfold':!isOpen}"
        ></span>
        <span class>江苏传智播客科技教育有限公司</span>
        <el-dropdown @command="clickItem">
          <span>
            <!-- 头像 -->
            <img src="user.photo" class="hear" alt />

            <span class="name">{{user.name}}</span>
            <i class="el-icon-arrow-down el-icon--right"></i>
          </span>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item command="setting" icon="el-icon-setting">个人设置</el-dropdown-item>
            <el-dropdown-item command="logout" icon="el-icon-unlock">退出登录</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </el-header>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import auth from "@/utils/auth";
import eventBus from "@/eventBus";
export default {
  name: "Layout",

  data() {
    return {
      isOpen: true,
      user: {
        name: "",
        photo: "",
      },
    };
  },
  methods: {
    //接受修改name

    setting() {
      this.$router.push("/setting");
    },
    logout() {
      //删除个人信息
      auth.delUser();
      this.$router.push("/login");
    },
    clickItem(command) {
      this[command]();
    },
  },
  created() {
    const { name, photo } = auth.getUser();
    this.user = { name, photo };
    eventBus.$on("userDataName", (data) => {
      this.user.name = data;
    });
    eventBus.$on("updateUserPhoto", (data) => {
      this.user.photo = data;
    });
  },
};
</script>

<style lang="less" scoped>
.container-layout {
  width: 100%;
  height: 100%;
  position: absolute;
  left: 0;
  top: 0;
  .el-aside {
    background-color: #023;
    .logo {
      width: 100%;
      height: 60px;
      background: #023 url(../assets/logo_admin.png) no-repeat center / 140px
        auto;
    }
    .mini-logo {
      background-image: url(../assets/logo_admin_01.png);
      background-size: 36px auto;
    }
  }
  .el-header {
    border-bottom: 1px solid #ddd;
    line-height: 60px;
    .icon {
      font-size: 24px;
      vertical-align: middle;
    }
    .text {
      margin-left: 10px;
      vertical-align: middle;
    }
    .el-dropdown {
      float: right;
      .hear {
        width: 30px;
        height: 30px;
        vertical-align: middle;
      }
      .name {
        vertical-align: middle;
        color: #333;
        font-weight: 700;
        margin-left: 10px;
      }
    }
  }
}
</style>
